// GUI.cs created with MonoDevelop
// User: kenw at 10:26 PM 6/3/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//

using System;
using System.Collections.Generic;
using Gtk;
using Glade;

namespace QDG.Powerball
{
	
	
	public class GUI
	{
		private List<Board> boards = null;
		private Board play = null;

		[Widget] Window MainWindow;
		[Widget] Entry eNbrOfBoards;
		[Widget] CheckButton cbPowerPlay;
		[Widget] Entry eTotalWinnings;
		
		[Widget] Frame fYourNumbers;
		
		[Widget] Entry ePBWins;
		public int PBWins
		{
			get {throw new NotImplementedException();}
			set {ePBWins.Text = value.ToString();}
		}
		
		[Widget] Entry ePBAmount;
		public int PBAmount
		{
			get {throw new NotImplementedException();}
			set {ePBAmount.Text = value.ToString();}
		}
		
		[Widget] Entry ePB1Wins;
		public int PB1Wins
		{
			get {throw new NotImplementedException();}
			set {ePB1Wins.Text = value.ToString();}
		}
		
		[Widget] Entry ePB1Amount;
		public int PB1Amount
		{
			get {throw new NotImplementedException();}
			set {ePB1Amount.Text = value.ToString();}
		}
		
		[Widget] Entry ePB2Wins;
		public int PB2Wins
		{
			get {throw new NotImplementedException();}
			set {ePB2Wins.Text = value.ToString();}
		}
		
		[Widget] Entry ePB2Amount;
		public int PB2Amount
		{
			get {throw new NotImplementedException();}
			set {ePB2Amount.Text = value.ToString();}
		}
		
		[Widget] Entry e3Wins;
		public int Ball3Wins
		{
			get {throw new NotImplementedException();}
			set {e3Wins.Text = value.ToString();}
		}
		
		[Widget] Entry e3Amount;
		public int Ball3Amount
		{
			get {throw new NotImplementedException();}
			set {e3Amount.Text = value.ToString();}
		}
		
		[Widget] Entry ePB3Wins;
		public int PB3Wins
		{
			get {throw new NotImplementedException();}
			set {ePB3Wins.Text = value.ToString();}
		}
		
		[Widget] Entry ePB3Amount;
		public int PB3Amount
		{
			get {throw new NotImplementedException();}
			set {ePB3Amount.Text = value.ToString();}
		}
		
		[Widget] Entry e4Wins;
		public int Ball4Wins
		{
			get {throw new NotImplementedException();}
			set {e4Wins.Text = value.ToString();}
		}
		
		[Widget] Entry e4Amount;
		public int Ball4Amount
		{
			get {throw new NotImplementedException();}
			set {e4Amount.Text = value.ToString();}
		}
		
		[Widget] Entry ePB4Wins;
		public int PB4Wins
		{
			get {throw new NotImplementedException();}
			set {ePB4Wins.Text = value.ToString();}
		}
		
		[Widget] Entry ePB4Amount;
		public int PB4Amount
		{
			get {throw new NotImplementedException();}
			set {ePB4Amount.Text = value.ToString();}
		}
		
		
		[Widget] Entry e5Wins;
		public int Ball5Wins
		{
			get {throw new NotImplementedException();}
			set {e5Wins.Text = value.ToString();}
		}
		
		[Widget] Entry e5Amount;
		public int Ball5Amount
		{
			get {throw new NotImplementedException();}
			set {e5Amount.Text = value.ToString();}
		}
		
		[Widget] Entry eJackpot;

		[Widget] Entry eDraw1;
		[Widget] Entry eDraw2;
		[Widget] Entry eDraw3;
		[Widget] Entry eDraw4;
		[Widget] Entry eDraw5;
		[Widget] Entry eDrawPB;
		[Widget] Entry ePowerPlay;
		
		[Widget] Entry ePlays;
		[Widget] Entry eYears;
	
		[Widget] Button bPlay;
		[Widget] Button bQuit;

		Glade.XML gxml;
		Values values;

		public GUI(Values values)
		{
			gxml = new XML("powerball.glade", "MainWindow");
			gxml.Autoconnect(this);
			this.values = values;
			
			try
			{
				int nbrOfBoards = 1;

				eNbrOfBoards.Changed += OnNbrOfBoardsChanged;
				
				eNbrOfBoards.Text = Convert.ToString(nbrOfBoards);
				
				bPlay.Clicked += RunSimulation;
				bQuit.Clicked += OnQuitClicked;
				while (Application.EventsPending())
					Application.RunIteration();
			}
			catch(Exception e)
			{
				Console.WriteLine("Error running application: {0}", e);
				Environment.Exit(1);
			}

		}
	
		public void OnQuitClicked(object o, EventArgs ea)
		{
			doCloseApplication();
		}
		
		
		private void OnNbrOfBoardsChanged(object o, EventArgs ea)
		{
			int nbrOfBoards = 0;
			Entry eNbrOfBoards = o as Entry;
			Console.WriteLine("OnNbrOfBoardsChanged: name: {0}, Text: \"{1}\"", eNbrOfBoards.Name, eNbrOfBoards.Text);
			
			if (String.IsNullOrEmpty(eNbrOfBoards.Text))
				return;
			
			nbrOfBoards = Int32.Parse(eNbrOfBoards.Text);
			
			Console.WriteLine("  nbrOfBoards {0}", nbrOfBoards);
			
			if (nbrOfBoards == 0)
				return;
			
			if (nbrOfBoards <=0)
				nbrOfBoards = 1;
			
			if (nbrOfBoards > 10)
				nbrOfBoards = 10;
			
			//Application.Invoke(delegate{eNbrOfBoards.Text = Convert.ToString(nbrOfBoards);});
			
			Console.WriteLine("number of boards = {0}", nbrOfBoards);
			
			boards = null;
			boards = new List<Board>(nbrOfBoards);
			
			VBox vbox = new VBox();
			
			for (int i = 0; i < nbrOfBoards; i++)
			{
				boards.Add(new Board(i));
				HBox hbox = new HBox(false, 0);
				hbox.Homogeneous = false;
				hbox.Spacing = 20;
				hbox.HeightRequest = 40;
				
				for (int j = 0; j < 6; j++)
					hbox.Add(boards[i].entries[j]);
				hbox.Add(boards[i].qp);
				vbox.Add(hbox);
			}
			
			fYourNumbers.Child.Destroy();
			fYourNumbers.Add(vbox);
			fYourNumbers.ShowAll();
		}
		
		public void RunSimulation(object o, EventArgs ea)
		{
		
			int nbrOfBoards = values.NbrOfBoards;
			int costToPlay = 0;

			Console.WriteLine("RunSimulation");
			int jackpot = 15000000;			
			nbrOfBoards = Int32.Parse(eNbrOfBoards.Text);
			costToPlay = cbPowerPlay.Active ? nbrOfBoards * 2 : nbrOfBoards;

			eJackpot.Text = jackpot.ToString();
			
			Console.WriteLine("nbrOfBoards: {0}, costToPlay: {1}", nbrOfBoards, costToPlay);
			
			int winnings = 0;
			int curWinnings = 0;
			


			while (true)
			{
				Console.WriteLine("in loop");
				winnings  = Int32.Parse(eTotalWinnings.Text) - costToPlay;
				eTotalWinnings.Text = Convert.ToString(winnings);
				
				Console.WriteLine("calling MakeDrawing()");
				MakeDrawing();
				
				curWinnings = 0;
				
				Console.WriteLine("Updating current winnings, play: {0}, boards: {1}", play, boards);
				
				for (int i = 0; i < nbrOfBoards; i++)
				{
					curWinnings += play.CompareBoard(boards[i]);
				}
				Console.WriteLine("updating total winnings");
				eTotalWinnings.Text = Convert.ToString(winnings + curWinnings);

			}
		}

		public void MakeDrawing()
		{
			if (play != null)
				play = null;
			int nbrOfBoards = values.NbrOfBoards;
			
			play = new Board(nbrOfBoards+1);
			
			play.qp.Click();
			Gtk.Application.Invoke(delegate{eDraw1.Text = play.entries[0].Text;});
			Gtk.Application.Invoke(delegate{eDraw2.Text = play.entries[1].Text;});
			Gtk.Application.Invoke(delegate{eDraw3.Text = play.entries[2].Text;});
			Gtk.Application.Invoke(delegate{eDraw4.Text = play.entries[3].Text;});
			Gtk.Application.Invoke(delegate{eDraw5.Text = play.entries[4].Text;});
			Gtk.Application.Invoke(delegate{eDrawPB.Text = play.entries[5].Text;});
			
			
			
		}				

		public void OnChoosePlay(object o, EventArgs args)
		{
			Console.WriteLine("OnChoosePlay start");
			Console.WriteLine("OnChoosePlay done: {0}", args.ToString());
		}
		
		public void OnMainWindowDeleteEvent(object o, DeleteEventArgs args)
		{
			Console.WriteLine("OnMainWindowDeleteEvent start");
			args.RetVal = doCloseApplication();
			Console.WriteLine("OnMainWindowDeleteEvent done: {0}", args.RetVal);
		}

		public void OnMainWindowDestroyEvent(object o, DestroyEventArgs args)
		{
			Console.WriteLine("OnMainWindowDestroyEvent start");
			args.RetVal = doCloseApplication();
			Console.WriteLine("OnMainWindowDestroyEvent done: {0}", args.RetVal);
		}

		public bool doCloseApplication()
		{
			Console.WriteLine("Application Quitting");
			// TODO: Any checks to determine whether or not the application
			// should stay active..
			//if( test-for-shutdown fails )
			//  return false;
			
			// By default, quit application and signal that we should
			// close the main window.
			Application.Quit();
			return true;
		}


	}

}
